﻿using System;
using System.Data;

namespace EXAM
{
    public partial class TestBoard : System.Web.UI.Page
    {
        string _code = "TESTPAGE";
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BUS.ParametersControl ctr = new BUS.ParametersControl();
                DTO.ParametersInfo inf = ctr.Get(_code, ref sErr);
                liContent.Text = inf.Description;

                BUS.GadgetItemsControl gadCtr = new BUS.GadgetItemsControl();
                DataTable dt = gadCtr.GetParentName("Testing", ref sErr);

                RadPanelBar1.DataSource = dt;
                RadPanelBar1.DataTextField = RadPanelBar1.DataValueField = "Parent";
                RadPanelBar1.DataBind();
                if (RadPanelBar1.Items.Count > 0)
                {
                    RadPanelBar1.Items[0].Selected = true;
                    RadPanelBar1.Items[0].Expanded = true;
                }
            }
        }

        protected void RadPanelBar1_ItemDataBound(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
        {
            if (e.Item != null)
            {
                if (e.Item is Telerik.Web.UI.RadPanelItem)
                {
                    DataRowView tmp = (DataRowView)((Telerik.Web.UI.RadPanelItem)e.Item).DataItem;
                    LoadChild((Telerik.Web.UI.RadPanelItem)e.Item, tmp["Parent"].ToString());
                }
            }
        }

        private void LoadChild(Telerik.Web.UI.RadPanelItem radPanelItem, string parent)
        {
            string typeGadget = "Testing";

            BUS.GadgetItemsControl itemCtr = new BUS.GadgetItemsControl();
            DataTable dtx = itemCtr.GetByGadget(parent, typeGadget, ref sErr);

            foreach (DataRow row in dtx.Rows)
            {
                string name = row["Name"].ToString();
                string link = row["Link"].ToString() == "" ? "#" : row["Link"].ToString();
                Telerik.Web.UI.RadPanelItem child = new Telerik.Web.UI.RadPanelItem(name, link);
                radPanelItem.Items.Add(child);
            }
        }
    }
}
